package debux.apps.mltr.model;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class MLTRDatabaseHelper extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "mltr.db";

	private static final int DATABASE_VERSION = 1;
	private static String DB_PATH = "/data/data/debux.apps.mltr.app/databases/";
	private static final String DB_NAME = "mltr.db";
	private Context context;
	private static final String DATABASE_CREATE_ALBUMS = "create table albums "
		+ "(_id integer primary key autoincrement, "
		+ "name text not null,"
		+ "genre text not null, "
		+ "released text not null,"
		+ "label text,"
		+ "link text);";

	private static final String DATABASE_CREATE_SONGS = "create table songs "
		+ "(_id integer primary key autoincrement, "
		+ "album_id integer , "
		+ "name text not null,"
		+ "genre text not null, "
		+ "length text not null,"
		+ "order_id integer,"
		+ "link text," 
		+ "lyrics text);";	
	
	public MLTRDatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
/*		Log.i("DatTX","Create MLTR database");
		// TODO Auto-generated method stub
		db.execSQL(DATABASE_CREATE_ALBUMS);
		db.execSQL(DATABASE_CREATE_SONGS);
		
		db.execSQL("INSERT INTO albums(name,genre,released,label,link) values('Michael Learns to Rock',			'Pop','September 1991','EMI, MCA','www.')");
		db.execSQL("INSERT INTO albums(name,genre,released,label,link) values('Colours',						'Pop','September 1991','EMI, MCA','www.')");
		db.execSQL("INSERT INTO albums(name,genre,released,label,link) values('Played on Pepper',				'Pop','September 1991','EMI, MCA','www.')");
		db.execSQL("INSERT INTO albums(name,genre,released,label,link) values('Paint My Love - Greatest Hits',	'Pop','September 1991','EMI, MCA','www.')");
		db.execSQL("INSERT INTO albums(name,genre,released,label,link) values('Nothing to Lose',				'Pop','September 1991','EMI, MCA','www.')");
		db.execSQL("INSERT INTO albums(name,genre,released,label,link) values('MLTR - Greatest Hits',			'Pop','September 1991','EMI, MCA','www.')");
		db.execSQL("INSERT INTO albums(name,genre,released,label,link) values('Blue Night',						'Pop','September 1991','EMI, MCA','www.')");
		db.execSQL("INSERT INTO albums(name,genre,released,label,link) values('Take Me to Your Heart',			'Pop','September 1991','EMI, MCA','www.')");
		db.execSQL("INSERT INTO albums(name,genre,released,label,link) values('Eternity',						'Pop','September 1991','EMI, MCA','www.')");
		
		 Init songs table 
		db.execSQL("INSERT INTO songs(album_id,name,genre,length,order_id,link,lyrics) values(1,'My Blue Angel','pop','3:55',1,'www.mp3.zing.vn','Sample lyrics')");		
		db.execSQL("INSERT INTO songs(album_id,name,genre,length,order_id,link,lyrics) values(1,'Looking at Love','pop','4:30',1,'www.mp3.zing.vn','Sample lyrics')");
		db.execSQL("INSERT INTO songs(album_id,name,genre,length,order_id,link,lyrics) values(1,'A Kiss in the Rain','pop','4:45',1,'www.mp3.zing.vn','Sample lyrics')");
		db.execSQL("INSERT INTO songs(album_id,name,genre,length,order_id,link,lyrics) values(1,'The Actor','pop','4:34',1,'www.mp3.zing.vn','Sample lyrics')");
		db.execSQL("INSERT INTO songs(album_id,name,genre,length,order_id,link,lyrics) values(1,'Messages','pop','4:32',1,'www.mp3.zing.vn','Sample lyrics')");
		db.execSQL("INSERT INTO songs(album_id,name,genre,length,order_id,link,lyrics) values(1,'I Still Carry on','pop','4:36',1,'www.mp3.zing.vn','Sample lyrics')");
		db.execSQL("INSERT INTO songs(album_id,name,genre,length,order_id,link,lyrics) values(1,'Crazy Dream','pop','3:59',1,'www.mp3.zing.vn','Sample lyrics')");
		db.execSQL("INSERT INTO songs(album_id,name,genre,length,order_id,link,lyrics) values(1,'African Queen','pop','4:10',1,'www.mp3.zing.vn','Sample lyrics')");
		db.execSQL("INSERT INTO songs(album_id,name,genre,length,order_id,link,lyrics) values(1,'Come on and Dance','pop','4:44',1,'www.mp3.zing.vn','Sample lyrics')");
		db.execSQL("INSERT INTO songs(album_id,name,genre,length,order_id,link,lyrics) values(1,'Let''s Build a Room','pop','3:25',1,'www.mp3.zing.vn','Sample lyrics')");
		db.execSQL("INSERT INTO songs(album_id,name,genre,length,order_id,link,lyrics) values(1,'Gone After Midnight','pop','4:10',1,'www.mp3.zing.vn','Sample lyrics')");
		*/
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
//		db.execSQL("DROP TABLE IF EXISTS albums");
//		db.execSQL("DROP TABLE IF EXISTS songs");		
//		onCreate(db);
	}
    public void createDataBase() throws IOException
    {
        boolean dbExist = checkDataBase();

        if(dbExist)
        {
            //do nothing - database already exist
        }else{

            this.getReadableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }

    }

    private boolean checkDataBase()
    {
        SQLiteDatabase checkDB = null;

        try
        {
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        }catch(SQLiteException e)
        {
            //database does't exist yet.
        }
        if(checkDB != null)
        {
            checkDB.close();
        }

        return checkDB != null ? true : false;
    }
    private void copyDataBase() throws IOException
    {
        //Open your local db as the input stream
        InputStream myInput = context.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0)
        {
            myOutput.write(buffer, 0, length);
        }
        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }
}
